package com.wlk.service.impl;

import com.wlk.mapper.TeachersMapper;
import com.wlk.pojo.Teachers;
import com.wlk.service.TeachersService;
import com.wlk.utils.Result;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TeachersServiceImpl implements TeachersService {
    @Resource
    private TeachersMapper teachersMapper;

    /**
     * @description: 添加老师信息
     * @author:
     * @date: 2024/6/10 下午11:53
     * @param:
     * @return:
     **/
    @Override
    public Result addTeacher(Teachers teacher) {
        // 对老师的必要信息进行验证，如姓名
        String teacherName = teacher.getTeacherName();
        if (teacherName == null || teacherName.trim().length() == 0) {
            return Result.noData("姓名不能为空");
        }
        int i = teachersMapper.addTeacher(teacher);
        if (i >0){
            return Result.ok("添加成功");
        }else {
            return Result.fail("添加失败");
        }

    }

    /**
     * @description: 获取老师列表
     * @author:
     * @date: 2024/6/10 下午11:53
     * @param:
     * @return:
     **/
    @Override
    public Result getTeachersList(int page,int size) {
        int offset = (page - 1) * size;
        List<Teachers> list = teachersMapper.getTeachersList(offset, size);
        if (list != null && list.size() > 0) {
            return Result.ok(list);
        }else {

            return Result.noData("暂无数据");
        }
    }

    /**
     * @description: 根据id删除老师信息
     * @author:
     * @date: 2024/6/10 下午11:54
     * @param:
     * @return:
     **/
    @Override
    public Result deleteTeacherById(int techerId) {
        int i = teachersMapper.deleteTeacher(techerId);
        if (i > 0) {
            return Result.ok("删除成功");
        } else {
            return Result.fail("删除失败");
        }
    }


    /**
     * @description: 更新老师信息
     * @author:
     * @date: 2024/6/10 下午11:56
     * @param:
     * @return:
     **/
    @Override
    public Result updateTeacher(Teachers teacher) {
        // 对老师信息进行验证
        String teacherName = teachersMapper.getTeacherNameById(teacher.getTeacherId());
        if (teacherName == null || teacherName.trim().length() == 0) {
          return Result.noData("修改的对象不存在");
        }
        int i = teachersMapper.updateTeacher(teacher);
        if (i > 0) {
            return Result.ok("修改成功");
        } else {
            return Result.fail("修改失败");
        }
    }

    @Override
    public Result deleteTeacher(@Param("teacherId") int teacherId) {
        int i = teachersMapper.deleteTeacher(teacherId);
        if (i > 0) {
            return Result.ok("删除成功");
        }else {
            return Result.fail("删除失败");
        }
    }

    }



